package com.example.hospitalapp;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PatientHomeActivity extends AppCompatActivity {

    private TextView txtPatientName, txtPatientDetails;
    private Button btnAppointment, btnMedicalRecords, btnViewAppointments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_home);

        txtPatientName = findViewById(R.id.patient_name);
        txtPatientDetails = findViewById(R.id.tvPatientDetails);
        btnAppointment = findViewById(R.id.btnAppointment);
        btnMedicalRecords = findViewById(R.id.btnMedicalRecords);
        btnViewAppointments = findViewById(R.id.btnViewAppointments);  // 新增按钮

        // 获取传递过来的病人信息
        Patient patient = (Patient) getIntent().getSerializableExtra("patient_info");
        if (patient != null) {
            // 显示病人信息
            txtPatientName.setText("姓名：" + patient.getName());
            txtPatientDetails.setText("手机号：" + patient.getPhoneNumber());
        }

        // 按钮点击事件
        btnAppointment.setOnClickListener(view -> {
            Intent intent = new Intent(PatientHomeActivity.this, AppointmentActivity.class);
            intent.putExtra("patient_info", patient);  // 传递患者信息到预约页面
            startActivity(intent);
        });

        btnMedicalRecords.setOnClickListener(view -> {
            // 调用查看病历接口并展示病历数据
            fetchMedicalRecords(patient.getPatientId());
        });

        // 点击查看预约按钮
        btnViewAppointments.setOnClickListener(view -> {
            Intent intent = new Intent(PatientHomeActivity.this, AppointmentListActivity.class);
            intent.putExtra("patient_info", patient);  // 传递患者信息到预约列表页面
            startActivity(intent);
        });
    }

    private void fetchMedicalRecords(int patientId) {
        JSONObject params = new JSONObject();
        try {
            params.put("patientId", patientId);  // 传递患者ID查询病历
        } catch (Exception e) {
            e.printStackTrace();
        }

        ApiClient apiClient = new ApiClient(PatientHomeActivity.this);
        apiClient.postRequest("http://zhsj.autumnclouds.cn/medicalrecord/query", params, new ApiClient.VolleyCallback() {
            @Override
            public void onSuccess(JSONObject response) {
                try {
                    int code = response.getInt("code");
                    if (code == 200) {
                        // 获取病历数据
                        JSONArray dataArray = response.getJSONArray("data");
                        if (dataArray.length() > 0) {
                            // 创建一个列表存储所有病历
                            List<MedicalRecord> medicalRecords = new ArrayList<>();

                            // 遍历每个病历并添加到列表
                            for (int i = 0; i < dataArray.length(); i++) {
                                JSONObject recordData = dataArray.getJSONObject(i);
                                MedicalRecord medicalRecord = new MedicalRecord(
                                        recordData.getInt("recordId"),
                                        recordData.getInt("patientId"),
                                        recordData.getInt("doctorId"),
                                        recordData.getString("visitDate"),
                                        recordData.getString("diagnosis"),
                                        recordData.getString("treatment"),
                                        recordData.getString("type"),
                                        recordData.getString("allergicHistory"),
                                        recordData.getInt("isComplete"),
                                        recordData.getString("onsetDate"),
                                        recordData.getString("illnessHistory")
                                );

                                // 将病历添加到列表
                                medicalRecords.add(medicalRecord);
                            }

                            // 跳转到病历详情页面并传递所有病历
                            Intent intent = new Intent(PatientHomeActivity.this, MedicalRecordActivity.class);
                            intent.putExtra("medical_records", (Serializable) medicalRecords);  // 传递多个病历信息
                            startActivity(intent);
                        } else {
                            Toast.makeText(PatientHomeActivity.this, "未找到病历信息", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(PatientHomeActivity.this, "查询病历失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(PatientHomeActivity.this, "查询病历失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(String error) {

            }
        });
    }

}
